import axios from 'axios'
import { Toast } from 'vant';

// function httpForDetail(params) {
//     return axios({
//         url: `https://m.maizuo.com/gateway?filmId=${params}&k=7264282`,
//         headers: {
//            'X-Client-Info':'{"a":"3000","ch":"1002","v":"5.2.0","e":"1642682627198350179663873"}'
//             'X-Requested-With': 'XMLHttpRequest'
//         }
//     })
// }

// function httpForList() {
//     return axios({
//         url: 'https://m.maizuo.com/gateway?cityId=350500&pageNum=1&pageSize=10&type=1&k=3843052',
//         headers: {
//             'X-Host': 'mall.film-ticket.film.list',
//             'X-Requested-With': 'XMLHttpRequest',
//                'X-Client-Info':'{"a":"3000","ch":"1002","v":"5.2.0","e":"1642682627198350179663873"}'
//         }
//     })
// }
// export default {
//     httpForDetail,
//     httpForList
// }

// 第二种方法
const http = axios.create({
    baseURL: 'https://m.maizuo.com',
    timeout: 10000,
    headers: {
        'X-Client-Info': '{"a":"3000","ch":"1002","v":"5.2.0","e":"1642682627198350179663873"}'
    }
})

// axios 拦截器
//发请求之前拦截
http.interceptors.request.use(function(config) {
    // Do something before request is sent
    Toast.loading({
        message: '加载中...',
        forbidClick: true,
        duration: 0, //设置为0永远不消失
        loadingType: 'spinner'
    });
    return config;
}, function(error) {
    // Do something with request error
    return Promise.reject(error);
});

// Add a response interceptor   成功拿到数据后，在axios.then之前
http.interceptors.response.use(function(response) {
    // Any status code that lie within the range of 2xx cause this function to trigger
    // Do something with response data\
    Toast.clear()
    return response;
}, function(error) {
    // Any status codes that falls outside the range of 2xx cause this function to trigger
    // Do something with response error
    Toast.clear()
    return Promise.reject(error);
});

export default http